import 'dart:io';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../lib_baseview/global_notice/float/float_screen.dart';
import '../../lib_baseview/lib_baseview.dart';
import '../../lib_baseview/root_page.dart';
import '../../lib_chat/home/index.dart';
import '../../lib_runtime/global_config.dart';
import '../../m_discover/discover_home/discover_home_view.dart';
import '../../m_me/me_page/me_page_new.dart';
import '../../resource/language/generated/l10n.dart';
import '../home/home_page.dart';
import '../main_page_model.dart';
import '../mine/mine_page.dart';

///主页
class MainPage extends StatefulWidget {
  MainPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _MainPageState();
  }
}

class _MainPageState extends State<MainPage> {
  final GlobalKey stackKey = GlobalKey();
  final GlobalKey<NewHomePageState> homePageKey = GlobalKey();
  final GlobalKey<MinePageState> minePageKey = GlobalKey();

  @override
  void initState() {
    super.initState();
    GlobalConfig.hasEnterMainPage = true;
    GlobalConfig.timeEnterMain = GlobalConfig.serverTime;
    rootNavigatorState.currentContext!.read<MainPageModel>().initData();
    handleNotificationJump();
    refreshSetting();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      FloatScreen.init();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<MainPageModel>(
      builder: (BuildContext context, MainPageModel model, child) {
        return Scaffold(
          // key: model.mainPageScaffoldkey,
          resizeToAvoidBottomInset: false,
          backgroundColor: Colors.white,
          body: WillPopScope(
            onWillPop: () async {
              if (Platform.isAndroid) {
                if (Navigator.canPop(context)) {
                  return true;
                } else {
                  // JrNavigator.backDeskTop();
                }
                return false;
              } else {
                return true;
              }
            },
            child: Stack(
              children: [
                Container(color: model.currentBottomBarIndex == 2 ? Colors.white : const Color(0xFFF8F8FF)),
                IndexedStack(
                  index: model.currentBottomBarIndex,
                  children: [
                    NewHomePage(key: homePageKey),
                    // Container(),
                    DiscoverHomePage(),
                    // Container(),
                    const ChatHomePage(),
                    MePageNew(),
                    // Container(),
                    // MinePage(key: minePageKey),
                  ],
                ),
                model.isShowBottom
                    ? Align(
                        alignment: Alignment.bottomCenter,
                        child: Container(
                          decoration: BoxDecoration(
                            boxShadow: [
                              BoxShadow(
                                  color: Colors.black.withOpacity(0.06), offset: const Offset(0, -4), blurRadius: 12)
                            ],
                            color: Colors.white,
                          ),
                          child: SafeArea(
                            top: false,
                            child: Stack(
                              alignment: Alignment.topCenter,
                              children: [
                                BottomNavigationBar(
                                  type: BottomNavigationBarType.fixed,
                                  currentIndex: model.currentBottomBarIndex,
                                  onTap: (index) {
                                    model.onNavigationBarClick(index);
                                    bool refresh = model.currentBottomBarIndex == index;
                                    if (refresh) {
                                      // _refreshPage(model,index);
                                    }
                                  },
                                  backgroundColor: Colors.transparent,
                                  selectedLabelStyle: const TextStyle(color: Color(0xFF7a5ffc)),
                                  unselectedLabelStyle: const TextStyle(color: Color(0xFF766B79)),
                                  elevation: 0,
                                  selectedFontSize: 12.0,
                                  unselectedFontSize: 12.0,
                                  items: [
                                    BottomNavigationBarItem(
                                      backgroundColor: Colors.transparent,
                                      icon: image(
                                        'tab/tabbar_home_normal.png',
                                        width: 30,
                                        height: 30,
                                      ),
                                      activeIcon: image(
                                        'tab/tabbar_home_highlighted.png',
                                        width: 30,
                                        height: 30,
                                      ),
                                      label: T.current.tab_rooms,
                                    ),
                                    BottomNavigationBarItem(
                                      backgroundColor: Colors.transparent,
                                      icon: image(
                                        'tab/tabbar_discover_normal.png',
                                        width: 30,
                                        height: 30,
                                      ),
                                      activeIcon: image(
                                        'tab/tabbar_discover_highlighted.png',
                                        width: 30,
                                        height: 30,
                                      ),
                                      label: T.of(context).tab_discover,
                                    ),
                                    BottomNavigationBarItem(
                                      backgroundColor: Colors.transparent,
                                      icon: image(
                                        'tab/tabbar_chats_normal.png',
                                        width: 30,
                                        height: 30,
                                      ),
                                      activeIcon: image(
                                        'tab/tabbar_chats_highlighted.png',
                                        width: 30,
                                        height: 30,
                                      ),
                                      label: T.current.tab_chats,
                                    ),
                                    BottomNavigationBarItem(
                                      backgroundColor: Colors.transparent,
                                      icon: image(
                                        'tab/tabbar_mine_normal.png',
                                        width: 30,
                                        height: 30,
                                      ),
                                      activeIcon: image(
                                        'tab/tabbar_mine_highlighted.png',
                                        width: 30,
                                        height: 30,
                                      ),
                                      label: T.of(context).tab_me,
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ),
                      )
                    : Container(),
              ],
            ),
          ),
        );
      },
    );
  }

  refreshSetting() async {
    bool result = await GlobalConfig.refreshSettings();
    if (result && mounted) {
      setState(() {});
    }
  }

  void handleNotificationJump() {
    // MessageNotifier.instance.notificationClick = (message) {
    //   if (message.body.type == MessageType.TXT) {
    //     //单聊跳聊天页面
    //     RLog.r("main", "click notification=$message");
    //     ChatNewPage.show(targetId: message.conversationId!,chatRefer: ChatRefer.chat_list);
    //   }
    // };
  }

// void _refreshPage(MainPageModel model,int index) async{
//   if(index == 0){
//     await homePageKey.currentState?.refresh();
//   }else if(index == 2){
//     await minePageKey.currentState?.refresh();
//   }
//   model.resetBottomBarIndexState(index);
// }
}
